package cn.iocoder.yudao.module.erp.controller.admin.finance.financein;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.finance.financein.vo.FinanceInPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.financein.vo.FinanceInRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.financein.vo.FinanceInSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.financein.FinanceInDO;
import cn.iocoder.yudao.module.erp.service.finance.financein.FinanceInService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收入单")
@RestController
@RequestMapping("/erp/finance-in")
@Validated
public class FinanceInController {

    @Resource
    private FinanceInService financeInService;


    @PostMapping("/create")
    @Operation(summary = "创建收入单")
    @PreAuthorize("@ss.hasPermission('erp:finance-in:create')")
    public CommonResult<Long> createFinanceIn(@Valid @RequestBody FinanceInSaveReqVO createReqVO) {
        return success(financeInService.createFinanceIn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收入单")
    @PreAuthorize("@ss.hasPermission('erp:finance-in:update')")
    public CommonResult<Boolean> updateFinanceIn(@Valid @RequestBody FinanceInSaveReqVO updateReqVO) {
        financeInService.updateFinanceIn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收入单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:finance-in:delete')")
    public CommonResult<Boolean> deleteFinanceIn(@RequestParam("id") Long id) {
        financeInService.deleteFinanceIn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收入单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:finance-in:query')")
    public CommonResult<FinanceInRespVO> getFinanceIn(@RequestParam("id") Long id) {
        return success(financeInService.getFinanceIn(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收入单分页")
    @PreAuthorize("@ss.hasPermission('erp:finance-in:query')")
    public CommonResult<PageResult<FinanceInRespVO>> getFinanceInPage(@Valid FinanceInPageReqVO pageReqVO) {
        PageResult<FinanceInDO> pageResult = financeInService.getFinanceInPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FinanceInRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收入单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:finance-in:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFinanceInExcel(@Valid FinanceInPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FinanceInDO> list = financeInService.getFinanceInPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收入单.xls", "数据", FinanceInRespVO.class,
                        BeanUtils.toBean(list, FinanceInRespVO.class));
    }

}